﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RaisingStudio.Data.Providers;
using SqlCe4Mvc3Application1.Models;

namespace SqlCe4Mvc3Application1.Controllers
{
    public class CommonBodyController : Controller
    {
        private DataContext dataContext;

        public CommonBodyController()
        {
            //this.dataContext = new DataContext();
            this.dataContext = DataContext.CreateInstance(AppDomain.CurrentDomain.BaseDirectory);
        }

        //
        // GET: /CommonBody/

        public ActionResult Index()
        {
            return View(this.dataContext.Query<Common_BodyPartRecord>());
        }
        
        //
        // GET: /CommonBody/Details/5

        public ActionResult Details(int id)
        {
            return View(this.dataContext.GetData<Common_BodyPartRecord>(id));
        }

        //
        // GET: /CommonBody/Create

        public ActionResult Create()
        {
            return View(new Common_BodyPartRecord() { });
        }

        //
        // POST: /CommonBody/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Common_BodyPartRecord common_BodyPartRecord)
        {
            try
            {
                common_BodyPartRecord.Id = Convert.ToInt32(this.dataContext.GetMax<Common_BodyPartRecord>(Common_BodyPartRecord._.Id)) + 1;
                // Insert Common_BodyPartRecord.
                int result = this.dataContext.Insert<Common_BodyPartRecord>(common_BodyPartRecord);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /CommonBody/Edit/5

        public ActionResult Edit(int id)
        {
            return View(this.dataContext.GetData<Common_BodyPartRecord>(id));
        }

        //
        // POST: /CommonBody/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Common_BodyPartRecord common_BodyPartRecord)
        {
            try
            {
                // Update CommonBody.
                common_BodyPartRecord.Id = id;
                int result = this.dataContext.Update<Common_BodyPartRecord>(common_BodyPartRecord);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            try
            {
                // Delete CommonBody.
                int result = this.dataContext.Delete<Common_BodyPartRecord>(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string Filter)
        {
            try
            {
                // Filter.
                string value = string.Format("%{0}%", Filter);
                IEnumerable<Common_BodyPartRecord> query = this.dataContext.Query<Common_BodyPartRecord>(Common_BodyPartRecord._.Text.Like(value));
                return View("Index", query);
            }
            catch
            {
                return View();
            }
        }

    }
}
